package cn.saas.test.greendao.gen;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import cn.saas.test.TowerBean;

import cn.saas.test.HoleBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "hole_detail".
*/
public class HoleBeanDao extends AbstractDao<HoleBean, Long> {

    public static final String TABLENAME = "hole_detail";

    /**
     * Properties of entity HoleBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property HoleId = new Property(1, Long.class, "holeId", false, "HOLE_ID");
        public final static Property Projectpk = new Property(2, String.class, "projectpk", false, "PROJECTPK");
        public final static Property Address = new Property(3, String.class, "address", false, "ADDRESS");
        public final static Property Adepth = new Property(4, Double.class, "adepth", false, "ADEPTH");
        public final static Property DeviceType = new Property(5, String.class, "deviceType", false, "DEVICE_TYPE");
        public final static Property Depth = new Property(6, Double.class, "depth", false, "DEPTH");
        public final static Property Distance = new Property(7, Double.class, "distance", false, "DISTANCE");
        public final static Property DistanceReason = new Property(8, String.class, "distanceReason", false, "DISTANCE_REASON");
        public final static Property HoleCode = new Property(9, String.class, "holeCode", false, "HOLE_CODE");
        public final static Property HoleType = new Property(10, String.class, "holeType", false, "HOLE_TYPE");
        public final static Property Holepk = new Property(11, String.class, "holepk", false, "HOLEPK");
        public final static Property UpdateTime = new Property(12, String.class, "updateTime", false, "UPDATE_TIME");
        public final static Property CreateTime = new Property(13, String.class, "createTime", false, "CREATE_TIME");
        public final static Property Username = new Property(14, String.class, "username", false, "USERNAME");
        public final static Property Latitude = new Property(15, Double.class, "latitude", false, "LATITUDE");
        public final static Property Longitude = new Property(16, Double.class, "longitude", false, "LONGITUDE");
        public final static Property Remark = new Property(17, String.class, "remark", false, "REMARK");
        public final static Property Status = new Property(18, Integer.class, "status", false, "STATUS");
        public final static Property TowerCode = new Property(19, String.class, "towerCode", false, "TOWER_CODE");
        public final static Property TowerSite = new Property(20, String.class, "towerSite", false, "TOWER_SITE");
        public final static Property Towerpk = new Property(21, String.class, "towerpk", false, "TOWERPK");
        public final static Property SyncStatus = new Property(22, int.class, "syncStatus", false, "SYNC_STATUS");
        public final static Property LastSyncTime = new Property(23, String.class, "lastSyncTime", false, "LAST_SYNC_TIME");
        public final static Property IsTowerHole = new Property(24, boolean.class, "isTowerHole", false, "IS_TOWER_HOLE");
        public final static Property TowerPk = new Property(25, String.class, "towerPk", false, "TOWER_PK");
        public final static Property Perpk = new Property(26, String.class, "perpk", false, "PERPK");
    }

    private DaoSession daoSession;

    private Query<HoleBean> towerBean_HoleBeansQuery;

    public HoleBeanDao(DaoConfig config) {
        super(config);
    }
    
    public HoleBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"hole_detail\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"HOLE_ID\" INTEGER," + // 1: holeId
                "\"PROJECTPK\" TEXT," + // 2: projectpk
                "\"ADDRESS\" TEXT," + // 3: address
                "\"ADEPTH\" REAL," + // 4: adepth
                "\"DEVICE_TYPE\" TEXT," + // 5: deviceType
                "\"DEPTH\" REAL," + // 6: depth
                "\"DISTANCE\" REAL," + // 7: distance
                "\"DISTANCE_REASON\" TEXT," + // 8: distanceReason
                "\"HOLE_CODE\" TEXT," + // 9: holeCode
                "\"HOLE_TYPE\" TEXT," + // 10: holeType
                "\"HOLEPK\" TEXT," + // 11: holepk
                "\"UPDATE_TIME\" TEXT," + // 12: updateTime
                "\"CREATE_TIME\" TEXT," + // 13: createTime
                "\"USERNAME\" TEXT," + // 14: username
                "\"LATITUDE\" REAL," + // 15: latitude
                "\"LONGITUDE\" REAL," + // 16: longitude
                "\"REMARK\" TEXT," + // 17: remark
                "\"STATUS\" INTEGER," + // 18: status
                "\"TOWER_CODE\" TEXT," + // 19: towerCode
                "\"TOWER_SITE\" TEXT," + // 20: towerSite
                "\"TOWERPK\" TEXT," + // 21: towerpk
                "\"SYNC_STATUS\" INTEGER NOT NULL ," + // 22: syncStatus
                "\"LAST_SYNC_TIME\" TEXT," + // 23: lastSyncTime
                "\"IS_TOWER_HOLE\" INTEGER NOT NULL ," + // 24: isTowerHole
                "\"TOWER_PK\" TEXT," + // 25: towerPk
                "\"PERPK\" TEXT);"); // 26: perpk
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"hole_detail\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, HoleBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long holeId = entity.getHoleId();
        if (holeId != null) {
            stmt.bindLong(2, holeId);
        }
 
        String projectpk = entity.getProjectpk();
        if (projectpk != null) {
            stmt.bindString(3, projectpk);
        }
 
        String address = entity.getAddress();
        if (address != null) {
            stmt.bindString(4, address);
        }
 
        Double adepth = entity.getAdepth();
        if (adepth != null) {
            stmt.bindDouble(5, adepth);
        }
 
        String deviceType = entity.getDeviceType();
        if (deviceType != null) {
            stmt.bindString(6, deviceType);
        }
 
        Double depth = entity.getDepth();
        if (depth != null) {
            stmt.bindDouble(7, depth);
        }
 
        Double distance = entity.getDistance();
        if (distance != null) {
            stmt.bindDouble(8, distance);
        }
 
        String distanceReason = entity.getDistanceReason();
        if (distanceReason != null) {
            stmt.bindString(9, distanceReason);
        }
 
        String holeCode = entity.getHoleCode();
        if (holeCode != null) {
            stmt.bindString(10, holeCode);
        }
 
        String holeType = entity.getHoleType();
        if (holeType != null) {
            stmt.bindString(11, holeType);
        }
 
        String holepk = entity.getHolepk();
        if (holepk != null) {
            stmt.bindString(12, holepk);
        }
 
        String updateTime = entity.getUpdateTime();
        if (updateTime != null) {
            stmt.bindString(13, updateTime);
        }
 
        String createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindString(14, createTime);
        }
 
        String username = entity.getUsername();
        if (username != null) {
            stmt.bindString(15, username);
        }
 
        Double latitude = entity.getLatitude();
        if (latitude != null) {
            stmt.bindDouble(16, latitude);
        }
 
        Double longitude = entity.getLongitude();
        if (longitude != null) {
            stmt.bindDouble(17, longitude);
        }
 
        String remark = entity.getRemark();
        if (remark != null) {
            stmt.bindString(18, remark);
        }
 
        Integer status = entity.getStatus();
        if (status != null) {
            stmt.bindLong(19, status);
        }
 
        String towerCode = entity.getTowerCode();
        if (towerCode != null) {
            stmt.bindString(20, towerCode);
        }
 
        String towerSite = entity.getTowerSite();
        if (towerSite != null) {
            stmt.bindString(21, towerSite);
        }
 
        String towerpk = entity.getTowerpk();
        if (towerpk != null) {
            stmt.bindString(22, towerpk);
        }
        stmt.bindLong(23, entity.getSyncStatus());
 
        String lastSyncTime = entity.getLastSyncTime();
        if (lastSyncTime != null) {
            stmt.bindString(24, lastSyncTime);
        }
        stmt.bindLong(25, entity.getIsTowerHole() ? 1L: 0L);
 
        String towerPk = entity.getTowerPk();
        if (towerPk != null) {
            stmt.bindString(26, towerPk);
        }
 
        String perpk = entity.getPerpk();
        if (perpk != null) {
            stmt.bindString(27, perpk);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, HoleBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long holeId = entity.getHoleId();
        if (holeId != null) {
            stmt.bindLong(2, holeId);
        }
 
        String projectpk = entity.getProjectpk();
        if (projectpk != null) {
            stmt.bindString(3, projectpk);
        }
 
        String address = entity.getAddress();
        if (address != null) {
            stmt.bindString(4, address);
        }
 
        Double adepth = entity.getAdepth();
        if (adepth != null) {
            stmt.bindDouble(5, adepth);
        }
 
        String deviceType = entity.getDeviceType();
        if (deviceType != null) {
            stmt.bindString(6, deviceType);
        }
 
        Double depth = entity.getDepth();
        if (depth != null) {
            stmt.bindDouble(7, depth);
        }
 
        Double distance = entity.getDistance();
        if (distance != null) {
            stmt.bindDouble(8, distance);
        }
 
        String distanceReason = entity.getDistanceReason();
        if (distanceReason != null) {
            stmt.bindString(9, distanceReason);
        }
 
        String holeCode = entity.getHoleCode();
        if (holeCode != null) {
            stmt.bindString(10, holeCode);
        }
 
        String holeType = entity.getHoleType();
        if (holeType != null) {
            stmt.bindString(11, holeType);
        }
 
        String holepk = entity.getHolepk();
        if (holepk != null) {
            stmt.bindString(12, holepk);
        }
 
        String updateTime = entity.getUpdateTime();
        if (updateTime != null) {
            stmt.bindString(13, updateTime);
        }
 
        String createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindString(14, createTime);
        }
 
        String username = entity.getUsername();
        if (username != null) {
            stmt.bindString(15, username);
        }
 
        Double latitude = entity.getLatitude();
        if (latitude != null) {
            stmt.bindDouble(16, latitude);
        }
 
        Double longitude = entity.getLongitude();
        if (longitude != null) {
            stmt.bindDouble(17, longitude);
        }
 
        String remark = entity.getRemark();
        if (remark != null) {
            stmt.bindString(18, remark);
        }
 
        Integer status = entity.getStatus();
        if (status != null) {
            stmt.bindLong(19, status);
        }
 
        String towerCode = entity.getTowerCode();
        if (towerCode != null) {
            stmt.bindString(20, towerCode);
        }
 
        String towerSite = entity.getTowerSite();
        if (towerSite != null) {
            stmt.bindString(21, towerSite);
        }
 
        String towerpk = entity.getTowerpk();
        if (towerpk != null) {
            stmt.bindString(22, towerpk);
        }
        stmt.bindLong(23, entity.getSyncStatus());
 
        String lastSyncTime = entity.getLastSyncTime();
        if (lastSyncTime != null) {
            stmt.bindString(24, lastSyncTime);
        }
        stmt.bindLong(25, entity.getIsTowerHole() ? 1L: 0L);
 
        String towerPk = entity.getTowerPk();
        if (towerPk != null) {
            stmt.bindString(26, towerPk);
        }
 
        String perpk = entity.getPerpk();
        if (perpk != null) {
            stmt.bindString(27, perpk);
        }
    }

    @Override
    protected final void attachEntity(HoleBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public HoleBean readEntity(Cursor cursor, int offset) {
        HoleBean entity = new HoleBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // holeId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // projectpk
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // address
            cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4), // adepth
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // deviceType
            cursor.isNull(offset + 6) ? null : cursor.getDouble(offset + 6), // depth
            cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7), // distance
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // distanceReason
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // holeCode
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // holeType
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // holepk
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // updateTime
            cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // createTime
            cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // username
            cursor.isNull(offset + 15) ? null : cursor.getDouble(offset + 15), // latitude
            cursor.isNull(offset + 16) ? null : cursor.getDouble(offset + 16), // longitude
            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17), // remark
            cursor.isNull(offset + 18) ? null : cursor.getInt(offset + 18), // status
            cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19), // towerCode
            cursor.isNull(offset + 20) ? null : cursor.getString(offset + 20), // towerSite
            cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21), // towerpk
            cursor.getInt(offset + 22), // syncStatus
            cursor.isNull(offset + 23) ? null : cursor.getString(offset + 23), // lastSyncTime
            cursor.getShort(offset + 24) != 0, // isTowerHole
            cursor.isNull(offset + 25) ? null : cursor.getString(offset + 25), // towerPk
            cursor.isNull(offset + 26) ? null : cursor.getString(offset + 26) // perpk
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, HoleBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setHoleId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setProjectpk(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setAddress(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setAdepth(cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4));
        entity.setDeviceType(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setDepth(cursor.isNull(offset + 6) ? null : cursor.getDouble(offset + 6));
        entity.setDistance(cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7));
        entity.setDistanceReason(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setHoleCode(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setHoleType(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setHolepk(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setUpdateTime(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setCreateTime(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setUsername(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
        entity.setLatitude(cursor.isNull(offset + 15) ? null : cursor.getDouble(offset + 15));
        entity.setLongitude(cursor.isNull(offset + 16) ? null : cursor.getDouble(offset + 16));
        entity.setRemark(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
        entity.setStatus(cursor.isNull(offset + 18) ? null : cursor.getInt(offset + 18));
        entity.setTowerCode(cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19));
        entity.setTowerSite(cursor.isNull(offset + 20) ? null : cursor.getString(offset + 20));
        entity.setTowerpk(cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21));
        entity.setSyncStatus(cursor.getInt(offset + 22));
        entity.setLastSyncTime(cursor.isNull(offset + 23) ? null : cursor.getString(offset + 23));
        entity.setIsTowerHole(cursor.getShort(offset + 24) != 0);
        entity.setTowerPk(cursor.isNull(offset + 25) ? null : cursor.getString(offset + 25));
        entity.setPerpk(cursor.isNull(offset + 26) ? null : cursor.getString(offset + 26));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(HoleBean entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(HoleBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(HoleBean entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "holeBeans" to-many relationship of TowerBean. */
    public List<HoleBean> _queryTowerBean_HoleBeans(Long holeId) {
        synchronized (this) {
            if (towerBean_HoleBeansQuery == null) {
                QueryBuilder<HoleBean> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.HoleId.eq(null));
                towerBean_HoleBeansQuery = queryBuilder.build();
            }
        }
        Query<HoleBean> query = towerBean_HoleBeansQuery.forCurrentThread();
        query.setParameter(0, holeId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getTowerBeanDao().getAllColumns());
            builder.append(" FROM hole_detail T");
            builder.append(" LEFT JOIN tower_info T0 ON T.\"HOLE_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected HoleBean loadCurrentDeep(Cursor cursor, boolean lock) {
        HoleBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        TowerBean towerBean = loadCurrentOther(daoSession.getTowerBeanDao(), cursor, offset);
        entity.setTowerBean(towerBean);

        return entity;    
    }

    public HoleBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<HoleBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<HoleBean> list = new ArrayList<HoleBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<HoleBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<HoleBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
